<script setup lang="ts">
import { ref } from 'vue'
import { DArrowRight } from '@element-plus/icons-vue'
import { getSmsCodeApi, getLoginByPwdApi, getLoginBySmsApi } from '@/apis/user'
import { useUserStore } from '@/stores/modules/user'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

// 切换登录方式
const loginType = ref(true)

// 用户名密码登录表单数据
const passwordLoginForm = ref({
  account: '18870920493',
  password: '123456',
  type: true,
})

// 用户名密码登录表单数据
const codeLoginForm = ref({
  mobile: '13888888888',
  code: '',
  type: true,
})

// 获取短信验证码
const getSmsCode = async () => {
  const res = await getSmsCodeApi(codeLoginForm.value.mobile)
  if (res.data.code === '1') {
    codeLoginForm.value.code = '123456'
  }
}

// 切换登录方式
const changeLoginType = () => {
  loginType.value = !loginType.value
}

// 用户密码登录
const passwordLogin = async () => {
  const res = await getLoginByPwdApi(passwordLoginForm.value)
  if (res.data.code === '1') {
    userStore.loginSuccess(res.data.result)
  }
}
// 手机验证码登录
const codeLogin = async () => {
  const res = await getLoginBySmsApi(codeLoginForm.value)
  if (res.data.code === '1') {
    userStore.loginSuccess(res.data.result)
  }
}
</script>

<template>
  <div class="box">
    <div class="logo"></div>
    <div class="togoods" @click="router.push('/')">
      <p>进入网站首页</p>
      <el-icon><DArrowRight color="#59a99c" /></el-icon>
    </div>
  </div>
  <div class="login-box" v-if="loginType">
    <div class="channge" @click="changeLoginType">去用验证码登录</div>
    <div class="form">
      <p>用户名密码登录</p>
      <el-form
        :model="passwordLoginForm"
        label-position="right"
        label-width="auto"
        style="max-width: 600px"
      >
        <!-- 账户 -->
        <el-form-item label="账户">
          <el-input v-model="passwordLoginForm.account" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码">
          <el-input v-model="passwordLoginForm.password" />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="passwordLoginForm.type"> 我已同意隐私条款和服务条款 </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" @click="passwordLogin">点击登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
  <div class="login-box" v-else>
    <div class="channge" @click="changeLoginType">去用密码登录</div>
    <div class="form">
      <p>手机验证码登录</p>
      <el-form
        :model="codeLoginForm"
        label-position="right"
        label-width="auto"
        style="max-width: 600px"
      >
        <!-- 账户 -->
        <el-form-item label="手机号">
          <el-input v-model="codeLoginForm.mobile" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="验证码">
          <el-input v-model="codeLoginForm.code" />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="codeLoginForm.type"> 我已同意隐私条款和服务条款 </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" @click="getSmsCode" v-if="codeLoginForm.code === ''">
            获取短信验证码
          </el-button>
          <el-button class="btn" @click="codeLogin" v-else>点击登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
  <div class="footer-box">
    <ul>
      <li style="border: transparent">关于我们</li>
      <li>帮助中心</li>
      <li>售后服务</li>
      <li>配送与验收</li>
      <li>商务合作</li>
      <li>搜索推荐</li>
      <li>友情连接</li>
    </ul>
    <p>CopyRight --- 小兔鲜</p>
  </div>
</template>

<style lang="scss" scoped>
.box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 150px;
  .logo {
    display: block;
    width: 200px;
    height: 132px;
    margin-left: 100px;
    background: url('@/asset/images/logo.png') no-repeat center 18px / contain;
  }
  .togoods {
    width: 200px;
    height: 30px;
    display: flex;
    align-items: center;
    cursor: pointer;
    p {
      margin-right: 5px;
    }
  }
}
.login-box {
  height: 488px;
  background: url('@/asset/images/login-bg.png') no-repeat center / cover;
  box-shadow: 10px 10px 10px rgba(152, 149, 149, 0.183);
  position: relative;
  .channge {
    width: 100px;
    height: 35px;
    background-color: #60b726;
    color: white;
    position: absolute;
    top: 80px;
    left: 1100px;
    z-index: 999;
    font-size: 13px;
    line-height: 36px;
    text-align: center;
    border-bottom-left-radius: 10px;
    cursor: pointer;
  }
  .form {
    width: 400px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 80px;
    left: 800px;

    p {
      width: 100%;
      height: 30px;
      text-align: center;
      margin-top: 5px;
      font-weight: 400;
      font-size: 18px;
    }
    .el-form {
      width: 100%;
      height: 250px;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      align-items: center;
      .el-form-item {
        width: 80%;
        margin: 0 auto;
        .btn {
          width: 300px;
          background-color: #27ae97;
          color: white;
        }
        // 修改 checkbox 主题色
        :deep(.el-checkbox) {
          --el-checkbox-checked-bg-color: #27ba9b; // 勾选背景色
          --el-checkbox-border-color: #27ba9b; // 边框颜色
          --el-checkbox-checked-text-color: #27ba9b; // 勾选时文字颜色
        }
      }
    }
  }
}
.footer-box {
  width: 100%;
  height: 100px;
  margin-top: 30px;
  display: flex;
  flex-direction: column;
  ul {
    margin: 20px auto;
    li {
      width: 80px;
      float: left;
      list-style: none;
      text-align: center;
      border-left: 1px solid rgba(163, 151, 134, 0.797);
      font-size: 12px;
      color: rgb(142, 142, 136);
    }
  }
  p {
    width: 100%;
    color: rgb(142, 142, 136);
    text-align: center;
  }
}
</style>
